﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace AllenalaMente.Games.Exchanges
{
    public partial class Exchanges : UserControl, GameInterface
    {
        private DispatcherTimer updateTimer;
        private Random random;

        Point p1 = new Point(100, 130);
        Point p2 = new Point(340, 130);
        Point p3 = new Point(570, 130);

        private int score;
        private int giuste, sbagliate;

        private int difficulty = 1;

        private UITimer gameTimer;

        public Exchanges()
        {
            InitializeComponent();

            // Initalize game
            random = new Random();
            score = 0;
            
            // Update timer
            updateTimer = new DispatcherTimer();
            updateTimer.Interval = TimeSpan.FromMilliseconds(20);
            updateTimer.Tick += new EventHandler(updateTimer_Tick);
            updateTimer.Start();

            cover1.position = p1;
            cover2.position = p2;
            cover3.position = p3;

            cover1.textBlockName.Text = "A";
            cover2.textBlockName.Text = "B";
            cover3.textBlockName.Text = "C";

            // Game timer
            gameTimer = new UITimer(60);
            LayoutRoot.Children.Add(gameTimer);
            gameTimer.timeOut += new EventHandler(game_TimeOut);
            gameTimer.Start();

            CreateCombination();
        }

        #region Metodi

        private List<int> numbers = new List<int>();

        private void CreateCombination()
        {
            cover1.MoveTo(p3);
            cover3.MoveTo(p2);
            cover2.MoveTo(p1);
        }

        private void CheckSolution()
        {

        }

        private void Mescola()
        {
            List<Point> positions = new List<Point>();
            positions.Add(p1);
            positions.Add(p2);
            positions.Add(p3);

            int index = random.Next(0, positions.Count);
            cover1.MoveTo(positions[index]);
            positions.Remove(positions[index]);

            index = random.Next(0, positions.Count);
            cover2.MoveTo(positions[index]);
            positions.Remove(positions[index]);

            index = random.Next(0, positions.Count);
            cover3.MoveTo(positions[index]);
            positions.Remove(positions[index]);

        }
        
        private void IncreaseScore(int amount)
        {
            score += amount;
            textBlockScore.Text = score.ToString();
        }

        #endregion

        #region Animations

        private void ShowSolution()
        {
        }

        public void Show(UIElement element)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = 0;
            doubleAnimation.To = 1;
            doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));

            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));
            Storyboard.SetTarget(doubleAnimation, element);

            Storyboard sb = new Storyboard();
            sb.Children.Add(doubleAnimation);
            sb.Begin();
        }

        public void hide(UIElement element)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = 1;
            doubleAnimation.To = 0;
            doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1.0));

            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));
            Storyboard.SetTarget(doubleAnimation, element);

            Storyboard sb = new Storyboard();
            sb.Children.Add(doubleAnimation);
            sb.Begin();
        }

        #endregion

        #region Eventi

        public event GameEvent GameEnd;

        int time = 0;
        private void updateTimer_Tick(object sender, EventArgs e)
        {
            time++;

            if (time == 100)
            {
                Mescola();
                time = 0;
            }
        }

        private void game_TimeOut(object sender, EventArgs e)
        {
            // Finito il tempo
            GameEventArgs gea = new GameEventArgs();
            gea.reason = GameCloseReason.Timeout;
            gea.score = this.score;
            gea.giuste = giuste;
            gea.sbagliate = sbagliate;
            if (GameEnd != null)
                GameEnd(this, gea);
        }

        #endregion
    }
}
